var checkZoneId;
var zoneId = "";
var routeId = "";

$(document).ready(function() {

	getZone();

	$("select#inputZone").change(function() {

		var vnZoneId = $("#inputZone").val();

		if (vnZoneId == "-1") {
			var options = '<option value="-1">-- เลือกเส้นทาง --</option>';
			$("select#inputRoute").html(options);
			$("#inputRoute").focus();
		} else {
			getRoute(vnZoneId);
			$("#inputRoute").focus();
		}

		checkZoneId = "";
		zoneId = "";
		routeId = "";

	});

	$("select#inputRoute").change(function() {
		checkZoneId = "";
		zoneId = "";
		routeId = "";
	});

	$('#selecctall').click(function(event) {
		if (this.checked) {
			$('.inputCheckbox').each(function() {
				this.checked = true;
			});
		} else {
			$('.inputCheckbox').each(function() {
				this.checked = false;
			});
		}
	});

	zoneId = $("#inputCheckZoneId").val();
	routeId = $("#inputCheckRouteId").val();
	
	//get Address such as province ,district and citycode
		getProvince();
		$("#inputContactProvinceCode").change(function() {
			 getDistrict();
			$("#inputContactDistrictCode").focus();
		 });
		 $("select#inputContactDistrictCode").change(function() {
			 getCityCode();
			 $("select#inputContactCityCode").focus();
		 });
});

function getZone() {
	$.getJSON('ZoneComponentAPISrvl', {}, function(result) {
		var options = '';
		options += '<option value="">-- เลือกเขต --</option>';
		for (var i = 0; i < result.values.length; i++) {
			var selected = "";
			if (zoneId != "" && routeId != "" && zoneId == result.values[i]) {
				selected = 'selected="selected"';
			}
			options += '<option value="' + result.values[i] + '" ' + selected
					+ '>' + result.messages[i] + '</option>';
		}
		$("select#inputZone").html(options);
		checkZoneId = document.getElementById("inputZone").value;
		if (checkZoneId != "-1") {
			getRoute(zoneId);
		}
	});
}

function getRoute($vnZoneId) {
	if ($vnZoneId != 'undefined' || $vnZoneId != "") {
		$.getJSON('RouteComponentAPISrvl', {
			zone_id : $vnZoneId
		}, function(result) {
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			for (var i = 0; i < result.values.length; i++) {
				var selected = "";
				if (zoneId != "" && routeId != ""
						&& routeId == result.values[i]) {

					selected = 'selected="selected"';
				}
				options += '<option value="' + result.values[i] + '" '
						+ selected + '>' + result.messages[i] + '</option>';
			}
			$("select#inputRoute").html(options);
			if (checkZoneId != "") {
				searchData();
			}
		});
	}
}

function getRoute2($vnZoneId,vnRoute) {
	if ($vnZoneId != 'undefined' || $vnZoneId != "") {
		$.getJSON('RouteComponentAPISrvl', {
			zone_id : $vnZoneId
		}, function(result) {
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			for (var i = 0; i < result.values.length; i++) {
				var selected = "";
				if (vnRoute == result.values[i]) {
					
					selected = 'selected="selected"';
					
				}else{
					selected = "";
				}
				options += '<option value="' + result.values[i] + '" '
						+ selected + '>' + result.messages[i] + '</option>';
			}
			$("select#inputRoute").html(options);
			
		});
	}
}





/**
 * JavaScript Component Month data List Box
 * 
 * @param $MonthID
 */
function SelectMonth($MonthID) {
	var selectMonth = '<select class="form-control" id="month" name="monht">';
	selectMonth += '<option value="">-- เลือกเดือน --</option>';
	selectMonth += '<option value="01">มกราคม</option>';
	selectMonth += '<option value="02">กุมภาพันธ์</option>';
	selectMonth += '<option value="03">มีนาคม</option>';
	selectMonth += '<option value="04">เมษายน</option>';
	selectMonth += '<option value="05">พฤษภาคม</option>';
	selectMonth += '<option value="06">มิถุนายน</option>';
	selectMonth += '<option value="07">กรกฎาคม</option>';
	selectMonth += '<option value="08">สิงหาคม</option>';
	selectMonth += '<option value="09">กันยายน</option>';
	selectMonth += '<option value="10">ตุลาคม</option>';
	selectMonth += '<option value="11">พฤษจิกายน</option>';
	selectMonth += '<option value="12">ธันวาคม</option></select>';
	$("#" + $MonthID).html(selectMonth);
}

/**
 * JavaScript Component Year data List Box
 */
function selectYear($elementYear) {
	var selectYear = "<select class='form-control' id='year' name='year'>";
	selectYear += "<option value=''> เลือกปี </option>";
	var objDate = new Date();
	var j = 1;
	for (var i = (objDate.getFullYear() - 2); i <= (objDate.getFullYear() + 2); i++) {
		selectYear += "<option value='" + (((objDate.getFullYear()+j) + 543)-2) + "'> " + (((objDate.getFullYear()+j) + 543)-2)+ " </option>";
		j++;
	}
	$("#"+$elementYear).html(selectYear);
}

function getProvince(province) {
	$.getJSON('ProvinceComponentAPISrvl', {
		province: "yes",
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกจังหวัด --</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(province==result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("#inputContactProvinceCode").html(options);
		 
	});
}

function getDistrict(province,district){
	var vaProvince ;
	if(isNaN(province) == true && isNaN(district) == true){
		vaProvince = document.getElementById("inputContactProvinceCode").value;	
	}else{
		vaProvince = province;
	}
	

	$.getJSON('DistrictComponentAPISrvl', {
		proVince: vaProvince,
	}, function(result) {
		var options = '';
		options += '<option value="">-- เลือกอำเภอ --</option>';
		var selected ="";
		for (var i = 0; i < result.values.length; i++) {
			if(district==result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#inputContactDistrictCode").html(options);
	});
}

function getCityCode(province,district,cityCode){
	var vaProvince ;
	var vaDistrict ;
	if(isNaN(province) == true && isNaN(district) == true && isNaN(cityCode) == true){
		vaProvince = document.getElementById("inputContactProvinceCode").value;	
		vaDistrict = document.getElementById("inputContactDistrictCode").value;
	}else{
		vaProvince = province;
		vaDistrict = district;
	}
	
	$.getJSON('CityCodeComponentAPISrvl', {
		proVince: vaProvince,
		disTrict: vaDistrict,
	}, function(result) {
		var selected="";
		var options = '';
		options += '<option value="">-- เลือกตำบล--</option>';
		for (var i = 0; i < result.values.length; i++) {
			if(cityCode == result.values[i]){
				selected = "selected='selected'";
			}else{
				selected ="";
			}
			options += '<option value="' + result.values[i] + '" '+selected+'>' + result.messages[i] +  '</option>';
		}
		 $("select#inputContactCityCode").html(options);
	});
}







